using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using EntLibEC.Cmn;
using EntLibEC.Info.Sale;
using EntLibEC.Biz;
using EntLibEC.Biz.Sale;

namespace EntLibEC.View.Sale
{
	/// <summary>
	/// Summary description for SOAddProduct.
	/// </summary>
	public partial class SOAddProduct : PageBase
	{
		
		private EntLibEC.Info.Sale.SOInfo soInfo;
		protected EntLibEC.View.Cmn.ProductType ucProductType;

		protected void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			itemGrid.PageSize = AppConst.PageSize;
			itemGrid.PagerStyle.PageButtonCount = AppConst.PageButtonCount;
			if(!IsPostBack)
			{
				this.GetInfo(true);
				ucProductType.BindProductType(true);
			}
			else
				this.GetInfo(false);
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.itemGrid.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.itemGrid_ItemCommand);
			this.itemGrid.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.itemGrid_PageIndexChanged);

		}
		#endregion

		protected void btnSearch_Click(object sender, System.EventArgs e)
		{
			BindGrid();
		}

		private void GetInfo(bool commend)
		{
			if(this.sInfo.sSO==null)
				Response.Redirect("SODetail.aspx");
			if(this.sInfo.sSO.SysNo!=AppConst.IntNull&&commend)
				this.sInfo.sSO = SOManager.GetInstance().LoadSO(this.sInfo.sSO.SysNo);
			this.soInfo = this.sInfo.sSO;
		}

		private void SaveInfo()
		{
			this.sInfo.sSO = soInfo;
		}

		private void BindGrid()
		{
			Hashtable paramHash = new Hashtable();
			if(this.txtProductID.Text.Trim()!=string.Empty)
				paramHash.Add("ProductID",this.txtProductID.Text.Trim());
			if(this.txtProductName.Text.Trim()!=string.Empty)
				paramHash.Add("ProductName",this.txtProductName.Text.Trim());

			if ( ucProductType.ProductTypeSysNo != AppConst.IntNull )
				paramHash.Add("ProductType", ucProductType.ProductTypeSysNo.ToString());

			DataSet ds = ConvertDs(SOManager.GetInstance().GetProductList(paramHash));

			base.SetDataGridPage(ds, itemGrid);

			itemGrid.DataSource = ds;
			itemGrid.DataBind();
			
			int rows = 0;
			if ( Util.HasMoreRow(ds) )
				rows = ds.Tables[0].Rows.Count;
			Assert(lblmsg, rows.ToString() + " record(s)", 1);
		}

		private DataSet ConvertDs(DataSet ds)
		{
			ds.Tables[0].Columns.Add("AddTOSO");
			ds.Tables[0].Columns.Add("AddGift");
			ds.Tables[0].Columns.Add("PriceShow");

			if ( !Util.HasMoreRow(ds))
				return ds;

			foreach(DataRow dr in ds.Tables[0].Rows)
			{
				dr["AddTOSO"] = "<img src=../images/cart.gif Border=0 Width=12 Height=12>";
				if((int)dr["IfHasGift"]>0)
				{
					if(this.soInfo.ItemHash.ContainsKey((int)dr["SysNo"]))
						dr["AddGift"] = "<a href=\"javascript:openWindowS2('SOAddGift.aspx?SysNo="+dr["SysNo"].ToString()+"')\"><img src='../images/Gift.gif' border=0 Width=14 Height=15></a>";
					else
						dr["AddGift"] = "<img src='../images/GiftOff.gif' border=0 Width=14 Height=15>";
				}
				dr["PriceShow"] = Util.TrimDecimalNull(dr["Price"]).ToString(AppConst.DecimalFormat);
			}
			return ds;
		}

		private void itemGrid_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			itemGrid.CurrentPageIndex = e.NewPageIndex;
			BindGrid();
		}

		protected void btnReturn_Click(object sender, System.EventArgs e)
		{
			if(soInfo.SysNo!=AppConst.IntNull)
				Response.Redirect("SODetail.aspx?SysNo="+soInfo.SysNo);
			else
				Response.Redirect("SODetail.aspx");
		}

		private void itemGrid_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			if(e.CommandName=="Add")
			{
				string qty = ((TextBox)e.Item.FindControl("txtQty")).Text.Trim();
				if(!Util.IsNaturalNumber(qty))
				{
					Assert(lblmsg,"Input quantity Error",-1);
					return;
				}
				SOItemInfo itemInfo = SOManager.GetInstance().LoadSOProduct((int)itemGrid.DataKeys[(int)e.Item.ItemIndex]);
				if(soInfo.SysNo!=AppConst.IntNull)
					itemInfo.SOSysNo=soInfo.SysNo;
				itemInfo.ProductType = (int)AppEnum.SOItemType.ForSale;
				itemInfo.Quantity = int.Parse(qty);
				itemInfo.DiscountAmt = 0m;
				try
				{
					SOManager.GetInstance().AddSOItem(itemInfo,soInfo);
					this.SaveInfo();
					this.BindGrid();
					Assert(lblmsg,"Added "+qty+" item to SaleOrder",1);
				}
				catch(BizException be)
				{
					Assert(lblmsg,be.Message,-1);
				}
				catch(Exception ex)
				{
					Assert(lblmsg,ex.Message,-1);
				}
			}
		}
	}
}
